<input type="hidden" id="new_poll_status" value="<?php echo $this->session->flashdata('new_poll_status'); ?>" />
<input type="hidden" id="poll_poll_id" value="<?php echo $this->session->flashdata('poll_poll_id'); ?>" />
<input type="hidden" id="poll_poll_hash" value="<?php echo $this->session->flashdata('poll_poll_hash'); ?>" />
<script type="text/javascript">
  $(document).ready(function() {
    $('#comment_message').wysihtml5();
    var new_poll_status = $.trim($('#new_poll_status').val());
    if (new_poll_status == 1) {
      toastr.success('Successfully created poll');
    }
    if (new_poll_status == 2) {
      toastr.error('Error occured. Try again !');
    }
    if (new_poll_status == 3) {
      toastr.success('Successfully voted');
    }
    var poll_poll_id = $.trim($('#poll_poll_id').val());
    var poll_poll_hash = $.trim($('#poll_poll_hash').val());
    if ((poll_poll_id) && (poll_poll_hash)) {
      send_notification('new_poll', poll_poll_id, poll_poll_hash);
    }
    load_new_options_replies();
    load_new_comments(true);
    update_likes_count();
  });
</script>

<div class="page" style="min-height: 500px;">
  <div class="page-container">
    <div class="container">
      <div class="row">
        <div class="span12 header">
          <div class="btn-group pull-right">
            <a href="<?php echo base_url('polls'); ?>" class="btn pull-right">View Polls</a>
          </div>
          <h4>Poll details</h4>
        </div>
        <div class="span3">
          <div class="filters">
            <span class="pull-right">
              <a title="<?php echo '@' . $poll_data['user_name']; ?>" style="text-decoration: none; color: #999999;" href="<?php echo base_url('profile/' . $poll_data['user_name']); ?>"><?php echo $poll_data['user_display_name']; ?></a>
            </span>
            <label for="cb3">Poll by</label>
            <?php if ($poll_data['poll_show_users'] == 1) { ?>
              <span class="pull-right" title="Other users can see your vote" style="cursor: pointer; color: yellowgreen;">Public</span>
            <?php } else { ?>
              <span class="pull-right" title="Other users cannot see your vote" style="cursor: pointer; color: tomato;">Private</span>
            <?php } ?>
            <label for="cb3">Poll type</label>
            <span class="pull-right" id="total_replies_count"><?php echo $poll_reply_count; ?></span>
            <label for="cb3">Total votes</label>
            <?php if ($poll_data['poll_status'] == 1) { ?>
              <span class="pull-right">Open</span>
            <?php } else { ?>
              <span class="pull-right" title="This poll is closed by owner !">Closed</span>
            <?php } ?>
            <label for="cb3">Poll status</label>
          </div>
        </div>
        <div class="span9">
          <form method="post" action="<?php echo base_url('polls/reply/' . $poll_data['poll_id']); ?>" class="form-horizontal" onsubmit="return confirm_action('Submit vote ?');">
            <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
            <p class="pull-right">
              <a style="text-decoration: none; cursor: pointer; color: salmon;" href="#poll_<?php echo $poll_data['poll_id']; ?>">&nbsp;&nbsp;#P<?php echo $poll_data['poll_id']; ?></a>
            </p>
            <p class="pull-right"><?php echo date('d - M - Y', strtotime($poll_data['poll_date'])); ?></p>
            <?php if (($this->current_user['user_id'] == $poll_data['user_id']) || ($this->is_admin)) { ?>
              <a onclick="return confirm_action('Delete poll ?');" style="text-decoration: none;" href="<?php echo base_url('polls/delete/' . $poll_data['poll_id']); ?>"><span style="color: #f8b9b7; float: right; margin-right: 10px;" title="Delete poll"><i class="icon-large icon-trash"></i></span></a>
              <?php if ($poll_data['poll_status'] == 1) { ?>
                <a onclick="return confirm_action('Close poll ?');" style="text-decoration: none;" href="<?php echo base_url('polls/close/' . $poll_data['poll_id']); ?>"><span style="color: #7932ea; float: right; margin-right: 10px;" title="Close poll"><i class="icon-large icon-minus-sign"></i></span></a>
              <?php } ?>
              <?php if ($poll_data['poll_status'] == 2) { ?>
                <a onclick="return confirm_action('Open poll ?');" style="text-decoration: none;" href="<?php echo base_url('polls/open/' . $poll_data['poll_id']); ?>"><span style="color: #7932ea; float: right; margin-right: 10px;" title="Open poll"><i class="icon-large icon-plus-sign"></i></span></a>
              <?php } ?>
            <?php } ?>
            <span style="float: right; margin-right: 10px;">
              <?php $get_user_like_status = get_user_like_status('poll', $poll_data['poll_id']); ?>
              <?php if ($get_user_like_status['status'] == 'yes') { ?>
                <a title="Unlike" style="text-decoration: none; cursor: pointer; color: yellowgreen;" id="like_button_poll_<?php echo $poll_data['poll_id']; ?>" onclick="return do_like_action('poll', '<?php echo $poll_data['poll_id']; ?>', '0');"><i class="icon-large icon-thumbs-down"></i></a>
              <?php } else { ?>
                <a title="Like" style="text-decoration: none; cursor: pointer; color: yellowgreen;" id="like_button_poll_<?php echo $poll_data['poll_id']; ?>" onclick="return do_like_action('poll', '<?php echo $poll_data['poll_id']; ?>', '1');"><i class="icon-large icon-thumbs-up"></i></a>
              <?php } ?>
            </span>
            <a class="update_like_count" title="Show users who like this" onclick="return show_liked_users('poll', '<?php echo $poll_data['poll_id']; ?>');" style="text-decoration: none; color: #777; float: right; margin-right: 10px; cursor: pointer;" id="like_count_poll_<?php echo $poll_data['poll_id']; ?>"><?php echo $get_user_like_status['total_count']; ?> Likes</a>
            <h4 style="word-break: normal; color: #0088CC"><?php echo $poll_data['poll_message']; ?></h4>
            <div style="height: 10px;"></div>
            <table class="table table-striped" width="100%">
              <tbody>
                <?php foreach ($poll_data['poll_options'] as $poll_option) { ?>
                  <tr id="poll_option_box_1_<?php echo $poll_option['poll_option_id']; ?>">
                    <td>
                      <label>
                        <input <?php if ($poll_reply_status == TRUE) { ?> readonly="readonly" onclick="return false;" <?php } ?> <?php if ($poll_reply_option == $poll_option['poll_option_id']) { ?> checked="checked" <?php } ?> style="margin-top: -2px;" class="poll_option_reply" name="poll_option_reply" type="radio" value="<?php echo $poll_option['poll_option_id']; ?>"/>
                        &nbsp;
                        <?php echo $poll_option['poll_option_message']; ?>
                        <?php if ($poll_option['poll_option_user_id'] != $poll_data['poll_user_id']) { ?>
                          <?php if ($poll_data['poll_show_users'] == 1) { ?>
                            <span style="float: right; font-size: smaller;">
                              By <?php echo $poll_option['user_display_name']; ?>
                            </span>  
                          <?php } ?>
                        <?php } ?>
                      </label>
                    </td>
                  </tr>
                <?php } ?>
                <tr id="new_poll_option_1" style="display: none;"></tr>
                <tr id="add_option_box" style="display: none;">
                  <td>
                    <label>
                      <input onclick="return $('#new_poll_option_msg').focus();" style="margin-top: -2px;" id="new_poll_option" class="poll_option_reply" name="poll_option_reply" type="radio" value="add"/>&nbsp;<input id="new_poll_option_msg" name="new_poll_option_msg" type="text" maxlength="250"/>
                    </label>
                  </td>
                </tr>
              </tbody>
            </table>

            <input type="hidden" id="poll_id" name="poll_id" value="<?php echo $poll_data['poll_id']; ?>"/>
            <?php if (!empty($poll_data['poll_options']) && is_array($poll_data['poll_options'])) { ?>
              <?php $last_poll_option_array = end($poll_data['poll_options']); ?>
              <input type="hidden" id="poll_option_last_id" name="poll_option_last_id" value="<?php echo $last_poll_option_array['poll_option_id']; ?>"/>
            <?php } else { ?>
              <input type="hidden" id="poll_option_last_id" name="poll_option_last_id" value="0"/>
            <?php } ?>

            <?php if ($poll_reply_status == FALSE) { ?>
              <?php if ($poll_data['poll_status'] == 1 && $poll_data['poll_add_option'] == 1) { ?>
                <div style="float: right;"> 
                  <div id="add_option">
                    <a title="Add a new option & vote" style="text-decoration: none; cursor: pointer;" onclick="return add_poll_option();">Add Option</a>
                  </div>
                  <div id="add_option_cancel" style="display: none;"> 
                    <a style="text-decoration: none; cursor: pointer;" onclick="return add_poll_option_cancel();">Cancel</a>
                  </div>
                </div>
              <?php } ?>
              <?php if ($poll_data['poll_status'] == 1) { ?> 
                <div class="control-group"> 
                  <button type="submit" class="btn" onclick="return validate_poll_reply();">Vote</button>
                </div>
              <?php } ?>
            <?php } ?>
          </form>
          <h4>Statistics</h4>
          <div class="header"></div>
          <?php if ($poll_data['poll_show_users'] == 1) { ?>
            <?php $show_user_title = 'Show users who voted this option'; ?>
          <?php } else { ?>
            <?php $show_user_title = 'You cannot see voted users because this poll is private'; ?>
          <?php } ?>
          <div class="widget">
            <table style="width:100%;">
              <?php foreach ($poll_data['poll_options'] as $poll_option) { ?>
                <?php $poll_option_reply_count = get_poll_reply_count($poll_option['poll_option_id']); ?>
                <?php $poll_option_percent = @(($poll_option_reply_count / $poll_reply_count) * 100); ?>
                <tr id="poll_option_box_2_<?php echo $poll_option['poll_option_id']; ?>">
                  <td class="bar-label">
                    <a title="<?php echo $show_user_title; ?>" style="text-decoration: none; cursor: pointer;" onclick="return show_poll_users('<?php echo $poll_data['poll_id']; ?>', '<?php echo $poll_option['poll_option_id']; ?>');">
                      <?php echo $poll_option['poll_option_message']; ?>
                    </a>
                  </td>
                  <td class="bar-number" id="option_bar_count_<?php echo $poll_option['poll_option_id']; ?>"><?php echo $poll_option_reply_count; ?> votes</td>
                  <td id="option_bar_title_<?php echo $poll_option['poll_option_id']; ?>" title="<?php echo ceil($poll_option_percent) . '%'; ?>" style="cursor: pointer;">
                    <div class="progress">
                      <div id="option_bar_width_<?php echo $poll_option['poll_option_id']; ?>" style="<?php echo 'width: ' . $poll_option_percent . '%'; ?>" class="bar"></div>
                    </div>
                  </td>
                </tr>
              <?php } ?>
              <tr id="new_poll_option_2" style="display: none;"></tr>
            </table>
          </div>
          <!-- Comments Section start -->
          <h4>Comments (<span id="total_comments_count"><?php echo $total_comments_count; ?></span>)<img id="loading_anim_blue" style="margin-left: 10px; margin-top: -5px; display: none;" src="<?php echo base_url('assets/img/loading_animation.gif'); ?>"/></h4>
          <div class="header"></div>
          <div class="messages">
            <div class="message">
              <a href="<?php echo base_url('profile/' . $this->current_user['user_name']); ?>"><img title="<?php echo $this->current_user['user_display_name']; ?>" src="<?php echo base_url('assets/photos/' . $this->current_user['user_image']); ?>" class="img-rounded pull-left" style="max-height: 60px; max-width: 60px;"/></a>
              <div class="message-body">
                <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
                <input type="hidden" id="comment_type" name="comment_type" value="<?php echo $comment_type; ?>" />
                <input type="hidden" id="comment_type_id" name="comment_type_id" value="<?php echo $comment_type_id; ?>" />
                <textarea id="comment_message" name="comment_message" placeholder="Enter your comment"></textarea>
                <a class="badge badge-info" id="malayalam_button" style="float: right; cursor: pointer; margin-right: 15px; text-decoration: none;" onclick="return write_malayalam('comment_message');">malaylam</a>
                <input id="post_comment_button" type="button" class="btn" value="Post" onclick="return post_comment('<?php echo $comment_type; ?>', '<?php echo $comment_type_id; ?>');"/>
              </div>
            </div>
            <div id="new_comments"></div>
            <?php $comment_last_id = 0; ?>
            <?php if (!empty($comments_data)) { ?>
              <?php foreach ($comments_data as $comment) { ?>
                <?php $post_id = 'C' . $comment['comment_id']; ?>
                <div class="message" id="comment_<?php echo $comment['comment_id']; ?>">
                  <a href="<?php echo base_url('profile/' . $comment['user_name']); ?>"><img title="<?php echo $comment['user_display_name']; ?>" src="<?php echo base_url('assets/photos/' . $comment['user_image']); ?>" class="img-rounded pull-left" style="max-height: 60px; max-width: 60px;"/></a>
                  <div class="message-body">
                    <p class="pull-right">
                      <a id="<?php echo $post_id; ?>"style="text-decoration: none; cursor: pointer; color: salmon;" href="#<?php echo $post_id; ?>">&nbsp;&nbsp;#<?php echo $post_id; ?></a>
                    </p>
                    <p class="pull-right">&nbsp;(<?php echo time_ago($comment['comment_date']); ?> ago)</p>
                    <p class="pull-right"><a title="Ignore post | <?php echo get_ignore_count('comment', $comment['comment_id']); ?> ignores !" style="text-decoration: none; cursor: pointer; color: yellowgreen;" id="ignore_comment_<?php echo $comment['comment_id']; ?>" onclick="return ignore_post('comment', '<?php echo $comment['comment_id']; ?>');"><i class="icon-large icon-flag"></i></a>&nbsp;</p>
                    <?php if (($this->current_user['user_id'] == $comment['user_id']) || ($this->is_admin)) { ?>
                      <a onclick="return delete_comment('<?php echo $comment['comment_id']; ?>');" style="text-decoration: none; cursor: pointer;" >
                        <span style="color: #f8b9b7; float: right; margin-right: 10px;" title="Delete comment"><i class="icon-large icon-trash"></i></span>
                      </a>
                    <?php } ?>
                    <?php if ($this->current_user['user_id'] != $comment['user_id']) { ?>
                      <a onclick="return reply_to_user('comment_message', '<?php echo $comment['user_name']; ?>', '#<?php echo $post_id; ?>');" style="text-decoration: none; cursor: pointer;"><span style="color: skyblue; float: right; margin-right: 10px;" title="Reply to this user"><i class="icon-large icon-retweet"></i></span></a>
                    <?php } ?>
                    <span style="float: right; margin-right: 10px;">
                      <?php $get_user_like_status = get_user_like_status('comment', $comment['comment_id']); ?>
                      <?php if ($get_user_like_status['status'] == 'yes') { ?>
                        <a title="Unlike" style="text-decoration: none; cursor: pointer; color: yellowgreen;" id="like_button_comment_<?php echo $comment['comment_id']; ?>" onclick="return do_like_action('comment', '<?php echo $comment['comment_id']; ?>', '0');"><i class="icon-large icon-thumbs-down"></i></a>
                      <?php } else { ?>
                        <a title="Like" style="text-decoration: none; cursor: pointer; color: yellowgreen;" id="like_button_comment_<?php echo $comment['comment_id']; ?>" onclick="return do_like_action('comment', '<?php echo $comment['comment_id']; ?>', '1');"><i class="icon-large icon-thumbs-up"></i></a>
                      <?php } ?>
                    </span>
                    <a class="update_like_count" title="Show users who like this" onclick="return show_liked_users('comment', '<?php echo $comment['comment_id']; ?>');" style="text-decoration: none; color: #777; float: right; margin-right: 10px; cursor: pointer;" id="like_count_comment_<?php echo $comment['comment_id']; ?>"><?php echo $get_user_like_status['total_count']; ?> Likes</a>
                    <h5>
                      <?php echo $comment['user_display_name']; ?>
                      &nbsp;
                      <span style="color: #777; font-size: smaller;">(@<?php echo $comment['user_name']; ?>)</span>
                      &nbsp;
                      <?php if (is_online($comment['user_id'])) { ?>
                        <img src="<?php echo site_url('assets/img/online_button_tiny.png'); ?>" title="Online"/>
                      <?php } else { ?>
                        <img src="<?php echo site_url('assets/img/offline_button_tiny.png'); ?>" title="Offline"/>
                      <?php } ?>
                    </h5>
                    <p style="word-break: normal;"><?php echo nl2br(parse_smileys($comment['comment_message'], base_url('assets/smileys'))); ?></p>
                  </div>
                </div>
              <?php } ?>
              <div id="old_comments"></div>
              <?php $comment_last_id = $comments_data[0]['comment_id']; ?>
              <?php $comment_array_end = end($comments_data); ?>
              <?php $comment_last_loaded_id = $comment_array_end['comment_id']; ?>
              <input type="hidden" id="comment_last_id" value="<?php echo $comment_last_id; ?>" />
              <input type="hidden" id="comment_last_loaded_id" value="<?php echo $comment_last_loaded_id; ?>" />
              <?php if (isset($total_comments_count)) { ?>
                <?php if ($total_comments_count > $comments_limit) { ?>
                  <a id="load_more" style="text-decoration: none; cursor: pointer; float: right; color: black" onclick="return load_old_comments();" title="Load more comments">
                    <img id="loading_animation" src="<?php echo base_url('assets/img/loading_black.gif'); ?>" style="display: none;"/>&nbsp;<b>more</b>&nbsp;<i class="icon-large icon-chevron-down"></i>
                  </a>
                <?php } ?>
              <?php } ?>
              <span style="display: none;" id="no_comments">No comments !</span>
            <?php } else { ?>
              <input type="hidden" id="comment_last_id" value="<?php echo $comment_last_id; ?>" />
              <div class="alert alert-error" id="no_comments">
                <strong>No comments !</strong>
              </div>
            <?php } ?>
          </div>
          <!-- Comments Section end -->
        </div>
      </div>
    </div>
  </div>
</div>